<?php



abstract class base_abstract
{
    function __construct($controller)
    {
        if (isset($_POST["PHPSESSID"])) {
    		session_id($_POST["PHPSESSID"]);
    	}
        session_start();
        
        $this->view = Smarty3::instance();
        $this->cache = Cache::$instance;
        $setting = _model('setting')->getList();
        foreach ($setting as $key => $val) {
            Config::set($val['name'], $val['value']);
        }
        
        if (isset($_SESSION['lang']) && $_SESSION['lang'] == 'zh') {
            $menu = array('contact_us'=>'联系我们', 'business'=>'服务商', 'about'=>'关于我们', 'index'=>'首页');
        } else {
            $menu = array('contact_us'=>'Contact us', 'business'=>'Business', 'about'=>'About us', 'index'=>'Home');
        }

        $this->view->assign('controller', $controller);
        $this->view->assign('menu', $menu); 
    }
    
    function get_tags()
    {
        static $tags = null;
        if ($tags != null) {
            return $tags;
        }
        
        if (!$tags = $this->cache->get('tags')) {
            $t = _model('tags')->getList('ORDER BY view_order ASC');

            foreach ($t as $key => $val) {
                if ($val['parent_id'] == 0) {
                    $tags[$val['tag_name']] = array();
                    foreach ($t as $k => $v) {
                        if ($v['parent_id'] == $val['id']) {
                            $tags[$val['tag_name']][] = $v;
                        }
                    }
                }
            }

            $this->cache->set('tags', $tags);
        }

        return $tags;
    }

    function redirect($url = '')
    {
        $site = SITE_URL;
        if (!$url) {
            $url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $site;
        }

        if (substr($url, 0, 4) != 'http') {
            if ($url{0} != '/') {
                $url = '/'.$url;
            }
            $url = $site.$url;
        }
        header('Location: ' . $url);
        exit;
    }

    function send_email($to, $subject, $content) {
    	try {

    		$mail             = new PHPMailer(true); // defaults to using php "mail()"
    	    $mail->CharSet    = "utf-8";
    	    $mail->Encoding   = "base64";

    	    $mail->SetFrom('admin@intelsalesadvisor.com');

    	    $mail->AddReplyTo("no-reply@intelsalesadvisor.com");

    	    $mail->AddAddress($to);

    	    $mail->Subject    = $subject;

    	    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";

    	    $mail->MsgHTML($content);

    	    $mail->Send();
        } catch (Exception $e) {
            return false;
        }

        return true;
    }
}

function get_user($id, $key)
{
   $info = _model('user')->getInfoById($id);
   return $info[$key];
}

function get_info($table, $id, $key) {
    static $info = array();
    if (!isset($info[$id.'_'.$table])) {
        $info[$id.'_'.$table] = _model($table)->read(array('id'=>$id));
    }

    if (!isset($info[$id.'_'.$table][$key])) {
        return '';
    }

    return $info[$id.'_'.$table][$key];
}

function get_file_type_img($type)
{
    $filename = ROOT_PATH.'/images/fileicons/'.$type.'.jpg';
    if (file_exists($filename)) {
        return '<img src="images/fileicons/'.$type.'.jpg" />';
    } else {
        return '';
    }
}

function check_rating($user_id, $asset_id) {
    return _model('vote_history')->read(array('user_id'=>$user_id, 'asset_id'=>$asset_id));
}
?>